package com.pengpai.pangucms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pengpai.pangucms.DTO.CurrentUser;
import com.pengpai.pangucms.VO.MenuVO;
import com.pengpai.pangucms.entity.SysPermission;
import com.pengpai.pangucms.entity.SysUserRole;
import com.pengpai.pangucms.mapper.SysPermissionMapper;
import com.pengpai.pangucms.mapper.SysUserMapper;
import com.pengpai.pangucms.service.ISysPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pengpai.pangucms.utils.TreeUtil;
import com.pengpai.pangucms.utils.UserUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜单权限表 服务实现类
 * </p>
 *
 * @author lijunjie
 * @since 2023-07-30
 */
@Service
@Transactional
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermission> implements ISysPermissionService {

    @Resource
    private SysPermissionMapper sysPermissionMapper;

    @Override
    public List<MenuVO> getMenuTree() {

//        Map<String, Object> map = new HashMap<>();
//        Map<String,Object> home = new HashMap<>();
//        Map<String,Object> logo = new HashMap<>();

        //当前登录用户
        CurrentUser currentUser = UserUtils.CurrentUser();

        Map<String, Object> params = new HashMap<>();

        params.put("userId",currentUser.getSysUser().getUserId());
        params.put("permissionTypeIgnore","B");
        List<SysPermission> sysPermissionList = sysPermissionMapper.getSysPermissionTree(params);

        List<MenuVO> menuInfo = new ArrayList<>();

        for (SysPermission s : sysPermissionList) {
            MenuVO menuVO = new MenuVO();
            menuVO.setId(s.getPermissionId());
            menuVO.setPid(s.getParentId());
            menuVO.setHref(s.getHref());
            menuVO.setTitle(s.getPermissionName());
            menuVO.setIcon(s.getIcon());
            menuVO.setOpenType(s.getTarget());
            if(s.getPermissionType().equalsIgnoreCase("N")){
                menuVO.setType(0);
            }
            menuInfo.add(menuVO);
        }

//        map.put("menuInfo", TreeUtil.toTree(menuInfo, 0));
//        home.put("title","首页");
//        home.put("href","/static/page/welcome-1.html?t=1");//控制器路由,自行定义
//        logo.put("title","后台管理系统");
//        logo.put("image","/static/images/logo.png");//静态资源文件路径,可使用默认的logo.png
//        map.put("homeInfo", home);
//        map.put("logoInfo", logo);
        return TreeUtil.toTree(menuInfo, 0);

    }

    @Override
    public boolean judgePmKey(SysPermission sysPermission) {
        QueryWrapper<SysPermission> wrapper = new QueryWrapper<>();
        wrapper.eq("permission_key", sysPermission.getPermissionKey());
        List<SysPermission> sysPermissions = sysPermissionMapper.selectList(wrapper);
        if(sysPermissions.size() == 0 || sysPermissions.get(0).getPermissionId() == sysPermission.getPermissionId()) return true;
        return false;
    }
}
